package com.cy.service.impl;

import com.cy.entity.Cart;
import com.cy.entity.Product;
import com.cy.mapper.CartMapper;
import com.cy.mapper.ProductMapper;
import com.cy.service.ICartService;
import com.cy.service.ex.AccessDeniedException;
import com.cy.service.ex.CarNotFoundException;
import com.cy.service.ex.InsertException;
import com.cy.service.ex.UpdateException;
import com.cy.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class ICartServiceImpl implements ICartService {
    /**
     * 购物车的业务层依赖于购物车的持久层和商品的持久层
     */

    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;


    @Override
    public void addToCart(Integer uid, Integer pid, Integer amount, String username) {
        //查询当前添加的购物是否在标准已存在
        Cart result = cartMapper.findByUidAndPid(uid, pid);
        Date date=new Date();
        //商品没有添加过
        if(result==null){
            //创建一个cart对象
            Cart cart=new Cart();
            cart.setUid(uid);
            cart.setPid(pid);
            cart.setNum(amount);
            //价格商品数据
            Product product = productMapper.findById(pid);
            cart.setPrice(product.getPrice());
            cart.setCreatedUser(username);
            cart.setModifiedTime(new Date());
            cart.setCreatedUser(username);
            cart.setCreatedTime(date);
            System.out.println(cart);
            Integer rows = cartMapper.insert(cart);
            if(rows!=1){
                throw new InsertException("插入数据产生未知的异常");
            }

        }
        else {
            //当前商品在购物车已存在 更新number即可
             Integer num= result.getNum() + amount;
             Integer rows=cartMapper.updateNumByCid(result.getCid(),num ,username,date);

             if(rows!=1){
                throw new UpdateException("更新时产生异常");
             }
        }
    }

    @Override
    public List<CartVO> getVOByUid(Integer uid) {
        return cartMapper.findVOByUid(uid);

    }

    @Override
    public Integer addNum(Integer cid, Integer uid, String username) {
        Cart result = cartMapper.findByCid(cid);
        if(result==null){
            throw new CarNotFoundException("数据不存在");
        }
        if(!result.getUid().equals(uid)){
            throw new AccessDeniedException("数据非法访问");
        }

        Integer num=result.getNum()+1;
        Integer rows=cartMapper.updateNumByCid(cid,num,username,new Date());
        if(rows!=1){
            throw new UpdateException("更新异常");
        }
        return num;
    }

    @Override
    public List<CartVO> getVOByCid( Integer uid,Integer[] cids) {
        List<CartVO> list = cartMapper.findVOByCids(cids);
        Iterator<CartVO> iterator = list.iterator();
        while (iterator.hasNext()){
            CartVO cartVO= iterator.next();
            if(!cartVO.getUid().equals(uid)){
                //从集合中移出这个元素
                list.remove(cartVO);
            }

        }
        return list;
    }
}
